#!/bin/sh
#
# Copyright (C) 2000-2022 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#
# Run a backup of the build directory but force it to have
#   a comm error, and check that it restarts correctly.
#
TestName="restart-accurate-job-test"
JobName=RestartJob
. scripts/functions

scripts/cleanup
scripts/copy-test-confs

setup_shstore # simulate shared storage if enabled

echo "$cwd/build/po"  > $tmp/file-list
echo "$cwd/build/src" >> $tmp/file-list

change_jobname NightlySave $JobName

$bperl -e "add_attribute('$conf/bacula-dir.conf', 'Accurate', 'yes', 'Job', 'RestartJob')"
$bperl -e "add_attribute('$conf/bacula-dir.conf', 'AllowIncompleteJobs', 'yes', 'Job', 'RestartJob')"
start_test
touch $cwd/build/po/testfile

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@output /dev/null
messages
@$out ${cwd}/tmp/log1.out
label storage=File volume=TestVolume001
setdebug options=c level=100 trace=1 hangup=100 client
@#setdebug level=20 dir
run job=$JobName yes
wait
messages
sql
select * from JobMedia;

quit
END_OF_DATA

run_bacula  

scripts/check_for_zombie_jobs storage=File

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@output /dev/null
@$out ${cwd}/tmp/log1.out
setdebug level=100 trace=1 hangup=50 client
@exec "sh -c 'touch $cwd/build/po/*'"
@exec "sh -c 'touch $cwd/build/src/dird/*'"
@exec "rm $cwd/build/po/testfile"
run job=$JobName yes
wait
messages
sql
select * from JobMedia;

@# 
@# now do a restore
@#
setdebug level=0 trace=1 client
@$out ${cwd}/tmp/log2.out
restore where=$tmp/bacula-restores storage=File select all done
yes
wait
messages
@$out ${cwd}/tmp/log3.out
list jobs
quit
END_OF_DATA

run_bconsole
scripts/check_for_zombie_jobs storage=File
stop_bacula

check_two_logs
$rscripts/diff.pl -s $tmp/bacula-restores/$cwd/build/po -d $cwd/build/po
if [ $? -ne 0 ]; then
    print_debug "ERROR: Found difference after restore"
    estat=1
fi

$rscripts/diff.pl -s $tmp/bacula-restores/$cwd/build/src -d $cwd/build/src
if [ $? -ne 0 ]; then
    print_debug "ERROR: Found difference after restore"
    estat=2
fi

nb_F=`awk -F '|' '/RestartJob.+F/ { gsub(/,/, ""); print $7}' $tmp/log3.out`
nb_I=`awk -F '|' '/RestartJob.+I/ { gsub(/,/, ""); print $7}' $tmp/log3.out`
nb1=`ls $cwd/build/po/* $cwd/build/src/dird/* | wc -l`
nb1=`expr $nb1 + 50`

if [ "$nb_I" -eq "$nb_F" ]; then
    print_debug "ERROR: Incremental saves too much files (nb Incr == nb Full)"
    estat=1
fi

if [ "$nb_I" -ge "$nb1" ]; then
    print_debug "ERROR: Incremental saves too much files (more than $nb1)"
    estat=1
fi

end_test
